﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.AdvTree;
using DevComponents.DotNetBar;

namespace project_assess.assess_relate
{
    public partial class predict_frm : Office2007Form
    {
        DataBase m_db;
        Mfrm mfrm;
        public predict_frm(Mfrm frm)
        {
            InitializeComponent();
            m_db = frm.m_db;
            mfrm = frm;
            load_every_tree();
        }

        private void btn_Click(object sender, EventArgs e)
        {
            ButtonX item = (ButtonX)sender;
            message_frm msg;
            if (mfrm.curprjid == null || mfrm.curprjid.Length == 0)
            {
                msg = new message_frm("没有选择项目,请先选择项目!", "错误", icon_img.warning);
                msg.ShowDialog(this);
                msg.Close();
                return;
            }
            string sql = "select [费用] from [assess_predict] where [项目编号]='"
               + mfrm.curprjid + "' and [控制成本]='班组提成'";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                msg = new message_frm("已完成核算\r\n是否继续添加", "询问", icon_img.query);
                msg.ShowDialog(this);
                if (msg.yesorno == false)
                {
                    msg.Close();
                    return;
                }
                else
                {
                    msg.Close();
                }
            }
            input_frm frm = new input_frm(m_db, item.Tag.ToString(), mfrm.curprjid);
            frm.ShowDialog(this);
            load_every_tree();

            
        }

        void load_every_tree()
        {
            string sql;
            if (mfrm.curprjid == null || mfrm.curprjid == "")
            {
                return;
            }
            DataTable dt;
            sql = "select [工作量] from [projects] where [项目编号]='"
                + mfrm.curprjid + "'";
            dt = m_db.GetDataSet(sql).Tables[0];
            double max = Convert.ToDouble(dt.Rows[0][0]) * mfrm.get_para("预算控制成本");
            double left, sum;

            sql = "select sum([费用]) from [assess_predict] where [项目编号]='"
                + mfrm.curprjid + "' and [控制成本]<>'项目提成' and [控制成本]<>'班组提成'";
            dt = m_db.GetDataSet(sql).Tables[0];
            try
            {
                sum = Convert.ToDouble(dt.Rows[0][0]);
            }
            catch
            {
                sum = 0;
            }
            r1.Text = "预算控制成本共计:" + max + ",已发生" + sum;
            r2.Text = "剩余:" + (max - sum).ToString();

            sql = "select [费用] from [assess_predict] where [项目编号]='"
                + mfrm.curprjid + "' and [控制成本]='班组提成'";
            dt = m_db.GetDataSet(sql).Tables[0];
            if (dt == null || dt.Rows.Count == 0)
            {
                r3.Text = "尚未完成核算";
            }
            else
            {
                left = Convert.ToDouble(dt.Rows[0][0]);
                if (left > 0)
                {

                    sql = "select [费用] from [assess_predict] where [项目编号]='"
                    + mfrm.curprjid + "' and [控制成本]='项目提成'";
                    dt = m_db.GetDataSet(sql).Tables[0];
                    sum = Convert.ToDouble(dt.Rows[0][0]);
                    r3.Text = "已完成核算,\r\n项目部提成:" + sum + ",班组提成:" + left;
                }
                else
                {
                    r3.Text = "已完成核算,需要扣除工资:" + left;
                }
            }

            //耗品费
            sql = "select [次数],[费用],[时间] from [assess_predict] where [项目编号]='"
                + mfrm.curprjid + "' and [控制成本]='耗品费' order by [次数]";
            dt = m_db.GetDataSet(sql).Tables[0];
            tree1.Nodes.Clear();
            tree1.Nodes.Add(new Node());
            tree1.Nodes[0].Text = mfrm.curprjid;
            tree1.Nodes[0].Expanded = true;

            left = max * mfrm.get_para("耗品费");


            sum = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Node n = new Node();
                n.Text = "第" + dt.Rows[i]["次数"].ToString() + "次";
                sum += Convert.ToDouble(dt.Rows[i]["费用"]);
                n.Cells.Add(new Cell(dt.Rows[i]["费用"].ToString()));
                n.Cells.Add(new Cell(dt.Rows[i]["时间"].ToString()));
                tree1.Nodes[0].Nodes.Add(n);
            }
            l1_1.Text = "低质量耗品共:" + left+",已发生:" + sum;
            l1_2.Text = "剩余:" + (left - sum).ToString();

            //仪器费
            sql = "select [次数],[费用],[时间] from [assess_predict] where [项目编号]='"
                + mfrm.curprjid + "' and [控制成本]='仪器费' order by [次数]";
            dt = m_db.GetDataSet(sql).Tables[0];
            tree2.Nodes.Clear();
            tree2.Nodes.Add(new Node());
            tree2.Nodes[0].Text = mfrm.curprjid;
            tree2.Nodes[0].Expanded = true;
            left = max * mfrm.get_para("仪器费");


            sum = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Node n = new Node();
                n.Text = "第" + dt.Rows[i]["次数"].ToString() + "次";
                sum += Convert.ToDouble(dt.Rows[i]["费用"]);
                n.Cells.Add(new Cell(dt.Rows[i]["费用"].ToString()));
                n.Cells.Add(new Cell(dt.Rows[i]["时间"].ToString()));
                tree2.Nodes[0].Nodes.Add(n);
            }
            l2_1.Text = "仪器费共:" + left + ",已发生:" + sum;
            l2_2.Text = "剩余:" + (left - sum).ToString();


            //交通费
            sql = "select [次数],[费用],[时间] from [assess_predict] where [项目编号]='"
                + mfrm.curprjid + "' and [控制成本]='交通费' order by [次数]";
            dt = m_db.GetDataSet(sql).Tables[0];
            tree3.Nodes.Clear();
            tree3.Nodes.Add(new Node());
            tree3.Nodes[0].Text = mfrm.curprjid;
            tree3.Nodes[0].Expanded = true;
            left = max * mfrm.get_para("交通费");


            sum = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Node n = new Node();
                n.Text = "第" + dt.Rows[i]["次数"].ToString() + "次";
                sum += Convert.ToDouble(dt.Rows[i]["费用"]);
                n.Cells.Add(new Cell(dt.Rows[i]["费用"].ToString()));
                n.Cells.Add(new Cell(dt.Rows[i]["时间"].ToString()));
                tree3.Nodes[0].Nodes.Add(n);
            }
            l3_1.Text = "交通费共:" + left + ",已发生:" + sum;
            l3_2.Text = "剩余:" + (left - sum).ToString();

            //协调费
            sql = "select [次数],[费用],[时间] from [assess_predict] where [项目编号]='"
                + mfrm.curprjid + "' and [控制成本]='协调费' order by [次数]";
            dt = m_db.GetDataSet(sql).Tables[0];
            tree4.Nodes.Clear();
            tree4.Nodes.Add(new Node());
            tree4.Nodes[0].Text = mfrm.curprjid;
            tree4.Nodes[0].Expanded = true;
            left = max * mfrm.get_para("协调费");


            sum = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Node n = new Node();
                n.Text = "第" + dt.Rows[i]["次数"].ToString() + "次";
                sum += Convert.ToDouble(dt.Rows[i]["费用"]);
                n.Cells.Add(new Cell(dt.Rows[i]["费用"].ToString()));
                n.Cells.Add(new Cell(dt.Rows[i]["时间"].ToString()));
                tree4.Nodes[0].Nodes.Add(n);
            }
            l4_1.Text = "协调费共:" + left + ",已发生:" + sum;
            l4_2.Text = "剩余:" + (left - sum).ToString();
        }


        //选择工程
        private void btn_chooseprj_Click(object sender, EventArgs e)
        {
            choose_prj frm = new choose_prj(mfrm);
            frm.ShowDialog(this);
            load_every_tree();
        }


        //核算所有的控制成本
        private void btn_assess_Click(object sender, EventArgs e)
        {
            message_frm frm;
            string sql;
            if (mfrm.curprjid == null || mfrm.curprjid == "")
            {
                frm = new message_frm("没有选择工程", "错误", icon_img.warning);
                frm.ShowDialog(this);
                frm.Close();
                return;
            }
            DataTable dt;
            sql = "select [工作量] from [projects] where [项目编号]='"
                + mfrm.curprjid + "'";
            dt = m_db.GetDataSet(sql).Tables[0];
            double max = Convert.ToDouble(dt.Rows[0][0]) * mfrm.get_para("预算控制成本");
            double left, sum, prjleft, teamleft;

            sql = "select sum([费用]) from [assess_predict] where [项目编号]='"
                + mfrm.curprjid + "' and [控制成本]<>'项目提成' and [控制成本]<>'班组提成'";
            dt = m_db.GetDataSet(sql).Tables[0];
            sum = Convert.ToDouble(dt.Rows[0][0]);
            left = max - sum;
            frm = new message_frm("是否要进行控制成本核算", "询问", icon_img.query);
            frm.ShowDialog(this);
            if (frm.yesorno == true)
            {
                if (left > 0)
                {
                    prjleft = left * mfrm.get_para("控制成本项目提成");
                    teamleft = left * mfrm.get_para("控制成本班组提成");

                    r3.Text = "已完成核算,\r\n项目部提成:" + prjleft + ",班组提成:" + teamleft;
              
                }
                else
                {
                    prjleft = 0;
                    teamleft = left * mfrm.get_para("控制成本班组提成");
                    r3.Text = "已完成核算,需要扣除工资:" + teamleft;
                }
                sql = "insert into [assess_predict] values('" + mfrm.curprjid + "','" + "项目提成" + "'," + prjleft
                + ",0,'" + DateTime.Now.ToShortDateString() + "','','','','')";
                m_db.ExecuteSQL(sql);
                sql = "insert into [assess_predict] values('" + mfrm.curprjid + "','" + "班组提成" + "'," + teamleft
               + ",0,'" + DateTime.Now.ToShortDateString() + "','','','','')";
                m_db.ExecuteSQL(sql);

            }
            frm.Close();
        }
    }
}
